package com.sxz.sbs.module.order;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sxz.sbs.R;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.constant.IntentConstant;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.order.adapter.OrderTypeAdapter;
import com.sxz.sbs.module.order.mode.OrderGroupBean;
import com.sxz.sbs.module.order.presenter.OrderGroupPresenter;
import com.sxz.sbs.module.order.view.OrderGroupView;
import com.sxz.sbs.module.search.SearchParkingActivity;
import com.sxz.sbs.router.RouterUrl;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hp
 * @date 2018/4/10
 */
@Route(path = RouterUrl.ORDER)
public class OrderHandleActivity extends BaseActivity<OrderGroupPresenter> implements OnTabSelectListener, OrderGroupView {

    @BindView(R.id.rely_more)
    RelativeLayout relyMore;
    @BindView(R.id.rely_title)
    RelativeLayout relyTitle;
    private PopupWindow mPopupWindow;
    private View decorView;
    private ViewPager vpOrder;
    private SlidingTabLayout tlOrder;
    private MyPagerAdapter mAdapter;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitle;
    private OrderTypeAdapter adapter;
    private OrderGroupBean bean;
    private List<OrderGroupBean> list = new ArrayList<>();
    private String keyWords = "";

    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("订单管理").showLeftReadyImg(R.drawable.ic_search_left).build().show();
        ARouter.getInstance().inject(this);
        getOrderGroupData();
        decorView = getWindow().getDecorView();
        tlOrder = ViewFindUtils.find(decorView, R.id.tl_order);
        vpOrder = ViewFindUtils.find(decorView, R.id.vp_order);
        /**
         * 更多操作
         */
        relyMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setPopupWindowType();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_orderlist;
    }

    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    protected OrderGroupPresenter createPresenter() {
        return new OrderGroupPresenter();
    }

    private void getOrderGroupData() {
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        mPresenter.getOrderGroup(map);
    }

    /**
     * @param data
     */
    @Override
    public void onOrderGroupSuccess(List<OrderGroupBean> data) {
        list = data;
        mTitle = new String[data.size()];
        mFragments.clear();
        for (int i = 0; i < data.size(); i++) {
            mTitle[i] = data.get(i).getName();
            int type = data.get(i).getType();
            mFragments.add(OrderListFragment.newInstance(type== 8? -1:type));
        }
        /**
         * 头部跟viewPage联动
         */
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        vpOrder.setAdapter(mAdapter);
        tlOrder.setViewPager(vpOrder);
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getIsRead() == 0) {
                tlOrder.showDot(i);
            }
        }
        vpOrder.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                LogUtils.e("---"+position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }



    @OnClick(R.id.iv_title_left_img)
    public void onViewClicked() {

        startActivity(new Intent(this,SearchParkingActivity.class)
                .putExtra(IntentConstant.SELECT_PARKING_KEY_WORD,keyWords)
                .putExtra(SearchParkingActivity.IS_FROM_ORDER,true));
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        if (requestCode == 1544 && resultCode == 1533) {
            keyWords = data.getStringExtra(IntentConstant.SELECT_PARKING_KEY_WORD);
            OrderListFragment.setKeyWords(keyWords);
            ((OrderListFragment)(mFragments.get(vpOrder.getCurrentItem()))).refresh();

        }
    }
    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

    private View getPopupWindowContentView() {
        // 一个自定义的布局，作为显示的内容
        int layoutId = R.layout.popup_content_layout;
        View contentView = LayoutInflater.from(this).inflate(layoutId, null);
        RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        RelativeLayout close = contentView.findViewById(R.id.rely_close);
        adapter = new OrderTypeAdapter(list);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                mPopupWindow.dismiss();
                vpOrder.setCurrentItem(position);
            }
        });
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopupWindow.dismiss();
            }
        });
        return contentView;
    }

    private void setPopupWindowType() {
        View contentView = getPopupWindowContentView();
        mPopupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int xOffset = relyMore.getWidth() / 2 - contentView.getMeasuredWidth() / 2;
        mPopupWindow.showAsDropDown(relyTitle, xOffset, 0);
    }

    @Override
    protected void onDestroy() {
        OrderListFragment.setKeyWords("");
        super.onDestroy();

    }
}
